package com.yxt.service;

import java.util.ArrayList;
import java.util.List;

import com.yxt.common.sys.CommConstant;
import com.yxt.dao.DBOperator;
import com.yxt.model.Station;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class StationService {
	private DBOperator dbHelper;

	public StationService(Context context) {
		dbHelper = new DBOperator(context);
	}

	public void saveStation(Station station) {
		SQLiteDatabase db = dbHelper.getDataBase();
		db.execSQL("insert into station (code,name,type,jd,wd,addr,river) "
				 + "values (? , ? , ? , ? , ? , ? , ? )",
		new Object[] {station.getCode(),station.getName(),station.getType(),station.getJd(),station.getWd(),station.getAddr(),station.getRiver()});
	}
	
	public Station getStationByCode(String code) {
		Station station = new Station();
		SQLiteDatabase db = dbHelper.getDataBase();
		Cursor cs = db.rawQuery("select code,name,type,jd,wd,addr,river from station where code = ? ",
				new String[] { String.valueOf(code) });
		
		if (cs.moveToNext()) {
			int index = 0;
			station.setCode(cs.getString(index++));
			station.setName(cs.getString(index++));
			station.setType(cs.getString(index++));
			station.setJd(cs.getString(index++));
			station.setWd(cs.getString(index++));
			station.setAddr(cs.getString(index++));
			station.setRiver(cs.getString(index++));
		}
		cs.close();
		return station;
	}
	
	public List<Station> getStationList() {
		List<Station> list = new ArrayList<Station>();
		SQLiteDatabase db = dbHelper.getDataBase();
		Cursor cs = db.rawQuery("select code,name,type,jd,wd,addr,river from station",null);		
		while (cs.moveToNext()) {
			int index = 0;
			Station station = new Station();
			station.setCode(cs.getString(index++));
			station.setName(cs.getString(index++));
			station.setType(cs.getString(index++));
			station.setJd(cs.getString(index++));
			station.setWd(cs.getString(index++));
			station.setAddr(cs.getString(index++));
			station.setRiver(cs.getString(index++));
			list.add(station);
		}
		cs.close();
		return list;
	}
	
	public List<Station> getStationListByCondition(String key,String value) {
		List<Station> list = new ArrayList<Station>();
		SQLiteDatabase db = dbHelper.getDataBase();
		StringBuffer sql = new StringBuffer();
		sql.append("select code,name,type,jd,wd,addr,river from station where ");
		
		if (CommConstant.Station.STATION_ALL.equals(key)) {
			String[] keys = {CommConstant.Station.STATION_NAME,CommConstant.Station.STATION_ADDR,CommConstant.Station.STATION_RIVER};
			for (int i = 0; i < keys.length; i++) {
				if (i > 0 ) {
					sql.append(" or ");
				}
				sql.append(keys[i]);
				sql.append(" like '%");
				sql.append(value);
				sql.append("%'");
			}
		} else {
			sql.append(key);
			sql.append(" like '%");
			sql.append(value);
			sql.append("%'");
		}
		Cursor cs = db.rawQuery(sql.toString(),null);		
		while (cs.moveToNext()) {
			int index = 0;
			Station station = new Station();
			station.setCode(cs.getString(index++));
			station.setName(cs.getString(index++));
			station.setType(cs.getString(index++));
			station.setJd(cs.getString(index++));
			station.setWd(cs.getString(index++));
			station.setAddr(cs.getString(index++));
			station.setRiver(cs.getString(index++));
			list.add(station);
		}
		cs.close();
		return list;
	}
		
	public void deleteStation() {
		SQLiteDatabase db = dbHelper.getDataBase();
		db.execSQL("delete from station");
	}

	public void closeDB() {
		dbHelper.close();
	}

}
